<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page extends FrontEndController {
    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->load->model('repo/product_model');
        $this->load->model('repo/product_highlight_model');
        $this->load->model('repo/shop_highlight_model');
        $aData['has_slider'] = TRUE;
        $aData['aLatest']    = $this->product_model->getLatestProducts();
        $aData['aHighlight'] = $this->product_highlight_model->getHighlightsProducts();
        $aData['aShops']     = $this->shop_highlight_model->getList(array('a.title', 'a.slug', 'c.image_folder', 'b.banner'), FALSE, SHOP_LIMIT);

        //var_dump($aData['aShops']);die();

        $this->renderView('pages/home', $aData);
    }

    public function getPage($pageslug) {
        $oPage = $this->page_model->getPageBySlug($pageslug);
        if ($oPage == null) {
            show_404();
        }
        $aData = array('pageTitle' => $oPage->getTitle(), 'oPage' => $oPage);
        $this->renderView('layout/page', $aData);
    }

    private function intialPaging($key, $countFunc, $limit, $count) {
        $this->mypaging->setCount($this->page_model->$countFunc($key));
        $this->mypaging->setLimit($limit);
        $this->mypaging->setMainURL(search_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function search($pageIndex=1) {
        $this->load->library('mypaging');
        if($this->input->is_ajax_request()){
            //return only tab-content
            $this->ajaxSearch($pageIndex);
        } else {
            //return tab-title + tab-content
            $this->directSearch($pageIndex);
        }
        return;
    }

    public function searchTab() {
        $aTotal = array();
        $key    = $this->input->get('s');
        $case   = $this->input->get('c');
        if($case == FALSE){
            $case = PRODUCT_SLUG;
        }
        $aTotal['post']    = $this->page_model->countSearchPost($key);
        $aTotal['raovat']  = $this->page_model->countSearchRaoVat($key);
        $aTotal['product'] = $this->page_model->countSearchProduct($key);
        $this->load->view('frontend/'.$this->config->item('front_theme').'/pages/search/index.html.php', array(
            'key'    => $key,
            'case'   => $case,
            'aTotal' => $aTotal
            ));
    }

    private function ajaxSearch($pageIndex){
        $result   = NULL;
        $template = NULL;
        $key      = $this->input->get('s');
        $case     = $this->input->get('c');
        switch ($case) {
            case POST_SLUG:
            $aTotalPost = $this->page_model->countSearchPost($key);
            $this->intialPaging($key, 'countSearchPost', POST_LIMIT, $aTotalPost);
            $offset   = ($pageIndex - 1) * POST_LIMIT;
            $result   = $this->page_model->searchPost($key, POST_LIMIT, $offset);
            $template = "post";
            break;
            case RAOVAT_SLUG:
            $aTotalRaoVat = $this->page_model->countSearchRaoVat($key);
            $this->intialPaging($key, 'countSearchRaoVat', RAO_VAT_LIMIT, $aTotalRaoVat);
            $offset   = ($pageIndex - 1) * RAO_VAT_LIMIT;
            $result   = $this->page_model->searchRaoVat($key, RAO_VAT_LIMIT, $offset);
            $template = "raovat";
            break;
            case FALSE:
            $case = PRODUCT_SLUG;
            case PRODUCT_SLUG:
            $aTotalProduct = $this->page_model->countSearchProduct($key);
            $this->intialPaging($key, 'countSearchProduct', PRODUCT_LIMIT, $aTotalProduct);
            $offset   = ($pageIndex - 1) * PRODUCT_LIMIT;
            $result   = $this->page_model->searchProduct($key, PRODUCT_LIMIT, $offset);
            $template = "product";
            default:
            break;
        }
        $aBar = array('s'=>$key);
        if($case!=false){ $aBar['c'] = $case; }
        $pagingBar = $this->mypaging->getBar($pageIndex, $aBar);
        $aReturn   = array(
            'pageTitle'  => 'Tìm kiếm',
            'search_key' => $key,
            'case'       => $case,
            'result'     => $result,
            'pagingBar'  => $pagingBar
            );
        $this->load->view('frontend/'.$this->config->item('front_theme').'/pages/search/'.$template.'.html.php', $aReturn);
    }

    private function directSearch($pageIndex){
        $result   = NULL;
        $template = NULL;
        $aTotal   = array();
        $key      = $this->input->get('s');
        $case     = $this->input->get('c');

        $aTotal['post']    = $this->page_model->countSearchPost($key);
        $aTotal['raovat']  = $this->page_model->countSearchRaoVat($key);
        $aTotal['product'] = $this->page_model->countSearchProduct($key);

        switch ($case) {
            case POST_SLUG:
            $this->intialPaging($key, 'countSearchPost', POST_LIMIT, $aTotal['post']);
            $offset   = ($pageIndex - 1) * POST_LIMIT;
            $result   = $this->page_model->searchPost($key, POST_LIMIT, $offset);
            $template = "post";
            break;
            case RAOVAT_SLUG:
            $this->intialPaging($key, 'countSearchRaoVat', RAO_VAT_LIMIT, $aTotal['raovat']);
            $offset   = ($pageIndex - 1) * RAO_VAT_LIMIT;
            $result   = $this->page_model->searchRaoVat($key, RAO_VAT_LIMIT, $offset);
            $template = "raovat";
            break;
            case FALSE:
            $case = PRODUCT_SLUG;
            case PRODUCT_SLUG:
            $this->intialPaging($key, 'countSearchProduct', PRODUCT_LIMIT, $aTotal['product']);
            $offset   = ($pageIndex - 1) * PRODUCT_LIMIT;
            $result   = $this->page_model->searchProduct($key, PRODUCT_LIMIT, $offset);
            $template = "product";
            default:
            break;
        }
        $aBar = array('s'=>$key);
        if($case!=false){
            $aBar['c'] = $case;
        }
        $pagingBar = $this->mypaging->getBar($pageIndex, $aBar);
        $aReturn   = array(
            'pageTitle'  => 'Tìm kiếm',
            'search_key' => $key,
            'case'       => $case,
            'result'     => $result,
            'pagingBar'  => $pagingBar,
            'template'   => $template,
            'aTotal'     => $aTotal
            );
        $this->renderView('pages/search/index', $aReturn);
    }

    public function login(){
        $data['no-sidebar'] = true;
        $this->renderView('static-pages/login', $data);
    }
}
?>